package com.soft.servlet;

import com.soft.entity.OrderReturnRecord;
import com.soft.service.Domain;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: 孔子雄
 * @Description: 退货处理Servlet
 * @DateTime: 2021/5/25 10:48
 */

@WebServlet(name = "ChuLiTuiHuoServlet", urlPatterns = "/ChuLiTuiHuo")
public class ChuLiTuiHuoServlet extends HttpServlet {
    private Domain domain;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        domain = new Domain();

        int orderId = Integer.parseInt(req.getParameter("orderId"));
        OrderReturnRecord ord = domain.getByOrderId(orderId);
        if (ord == null) {
            req.getSession().setAttribute("flag", 24);
            req.getRequestDispatcher("/OrderManageList").forward(req, resp);
        }
        else if (ord.getResult() ==0 || ord.getResult()==1) {
            req.setAttribute("Ord", ord);
            req.getRequestDispatcher("chulituihuo.jsp").forward(req, resp);
        } else {
            req.getSession().setAttribute("flag", 17);
            req.getRequestDispatcher("/OrderManageList").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        domain = new Domain();

        int OrdId = Integer.parseInt(req.getParameter("member.id"));
        OrderReturnRecord ord = domain.getById(OrdId);

        String result = req.getParameter("member.result");
        ord.setResult(result);

        if (domain.updateOrderReturnRecord(ord))
            req.getSession().setAttribute("flag", 15);
        else
            req.getSession().setAttribute("flag", 16);
        req.setAttribute("Ord", ord);
        req.getRequestDispatcher("chulituihuo.jsp").forward(req, resp);
    }
}
